/*
 * Kinetis porting
 * 
 * Author: Major Lin
 * 
*/

#ifndef __KINETIS_MAP__H_
#define __KINETIS_MAP__H_

#undef CPU_MAP_ATMEGA328P 
#define PORTING_CPU     KINETIS
#define SPINDLE_OFF_PWM 400

#define BOARD_DEBUG_UART_CLK_FREQ
#define BOARD_DEBUG_UART_BASEADDR   LPUART0

// Some useful constants
#define F_CPU                   (48000000)
#define TICKS_PER_MICROSECOND (F_CPU/1000000)
#define CYCLES_PER_ACCELERATION_TICK ((TICKS_PER_MICROSECOND*1000000)/ACCELERATION_TICKS_PER_SECOND)

#include "fsl_device_registers.h"


#define INTERRUPT_EITHER_EDGE   (0x0a)
#define M_PI                (3.141592653)

// port changes when X or Y or Z axis reaches limits
#define LIMIT_PORT          PORTD
#define LIMIT_GPIO          GPIOD
#define X_LIMIT_BIT         (0)
#define Y_LIMIT_BIT         (1)
#define Z_LIMIT_BIT         (2)
#define LIMIT_MASK          ((1<<X_LIMIT_BIT)|(1<<Y_LIMIT_BIT)|(1<<Z_LIMIT_BIT))

// used to control cool plate
#define COOLANT_FLOOD_PORT  PORTD
#define COOLANT_FLOOD_GPIO  GPIOD
#define COOLANT_FLOOD_BIT   (3)

// probe is used to calibrate probe to board surface
#define PROBE_PORT          PORTD
#define PROBE_GPIO          GPIOD
#define PROBE_BIT           (4)
#define PROBE_MASK          (1<<PROBE_BIT)

// Define step pulse output pins. NOTE: All step bit pins must be on the same port.
#define X_STEP_BIT      2  // Uno Digital Pin 2
#define Y_STEP_BIT      3  // Uno Digital Pin 3
#define Z_STEP_BIT      4  // Uno Digital Pin 4
#define STEP_MASK       ((1<<X_STEP_BIT)|(1<<Y_STEP_BIT)|(1<<Z_STEP_BIT)) // All step bits

//// Define step direction output pins. NOTE: All direction pins must be on the same port.
//#define DIRECTION_DDR     step_port.direction
//#define DIRECTION_PORT    step_port.value
#define X_DIRECTION_BIT   5  // Uno Digital Pin 5
#define Y_DIRECTION_BIT   6  // Uno Digital Pin 6
#define Z_DIRECTION_BIT   7  // Uno Digital Pin 7
#define DIRECTION_MASK    ((1<<X_DIRECTION_BIT)|(1<<Y_DIRECTION_BIT)|(1<<Z_DIRECTION_BIT)) // All direction bits

// Define stepper driver enable/disable output pin.
#define STEPPERS_DISABLE_DDR    step_port.direction
#define STEPPERS_DISABLE_PORT   step_port.value
#define STEPPERS_DISABLE_BIT    0  // Uno Digital Pin 8
#define STEPPERS_DISABLE_MASK   (1<<STEPPERS_DISABLE_BIT)

// Define spindle enable and spindle direction output pins.
#define SPINDLE_ENABLE_PORT   PORTD
#define SPINDLE_ENABLE_GPIO   GPIOD
// Z Limit pin and spindle PWM/enable pin swapped to access hardware PWM on Pin 11.
#ifdef VARIABLE_SPINDLE
    #ifdef USE_SPINDLE_DIR_AS_ENABLE_PIN
        // If enabled, spindle direction pin now used as spindle enable, while PWM remains on D11.
        #define SPINDLE_ENABLE_BIT    5  // Uno Digital Pin 13 (NOTE: D13 can't be pulled-high input due to LED.)
    #else
        #define SPINDLE_ENABLE_BIT    3  // Uno Digital Pin 11
    #endif
#else
    #define SPINDLE_ENABLE_BIT    4  // Uno Digital Pin 12
#endif
#ifndef USE_SPINDLE_DIR_AS_ENABLE_PIN
    #define SPINDLE_DIRECTION_PORT  PORTD
    #define SPINDLE_DIRECTION_GPIO  GPIOD
    #define SPINDLE_DIRECTION_BIT   5  // Uno Digital Pin 13 (NOTE: D13 can't be pulled-high input due to LED.)
#endif

// Define user-control controls (cycle start, reset, feed hold) input pins.
// NOTE: All CONTROLs pins must be on the same port and not on a port with other input pins (limits).
#define CONTROL_PORT      PORTC
#define CONTROL_GPIO      GPIOC
#define CONTROL_RESET_BIT         0  // Uno Analog Pin 0
#define CONTROL_FEED_HOLD_BIT     1  // Uno Analog Pin 1
#define CONTROL_CYCLE_START_BIT   2  // Uno Analog Pin 2
#define CONTROL_SAFETY_DOOR_BIT   1  // Uno Analog Pin 1 NOTE: Safety door is shared with feed hold. Enabled by config define.
#define CONTROL_MASK      ((1<<CONTROL_RESET_BIT)|(1<<CONTROL_FEED_HOLD_BIT)|(1<<CONTROL_CYCLE_START_BIT)|(1<<CONTROL_SAFETY_DOOR_BIT))
#define CONTROL_INVERT_MASK   CONTROL_MASK // May be re-defined to only invert certain control pins.

// Variable spindle configuration below. Do not change unless you know what you are doing.
// NOTE: Only used when variable spindle is enabled.
#define SPINDLE_PWM_MAX_VALUE     255 // Don't change. 328p fast PWM mode fixes top value as 255.
#ifndef SPINDLE_PWM_MIN_VALUE
#define SPINDLE_PWM_MIN_VALUE   1   // Must be greater than zero.
#endif
#define SPINDLE_PWM_OFF_VALUE     0
#define SPINDLE_PWM_RANGE         (SPINDLE_PWM_MAX_VALUE-SPINDLE_PWM_MIN_VALUE)

#endif